﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace syc.Sale
{
    public partial class 分厂上报辅料计划 : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.NianDu.Text = DateTime.Today.Year.ToString();
                this.YueFen.Text = DateTime.Today.Month.ToString();
                int fenchang = int.Parse(Request.QueryString["fenchang"]);
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                String strSql1 = "SELECT [ID], [品名]+' '+ [规格]+'('+[单位]+')' as [品名规格] FROM [材料]";
                String strSql2 = "Select [ID],[名称] from [生产单位]";
                SqlDataAdapter sda = new SqlDataAdapter(strSql1, Conn);
                SqlDataAdapter sda1 = new SqlDataAdapter(strSql2, Conn);
                DataSet ds = new DataSet();
                DataSet ds1 = new DataSet();
                sda.Fill(ds);
                sda1.Fill(ds1);
                this.FuLiao.DataSource = ds;
                this.FuLiao.DataBind();
                this.FenChang.DataSource = ds1;
                this.FenChang.DataBind();
                GridView1_Bounding(fenchang);
                DataBounding();               
            }         
        }
        public void GridView1_Bounding(int fenchang)
        {

            string sql = @"select 分厂上报辅料采购计划.ID,品名,规格,单位,上报计划采购量,上报日期 from 分厂上报辅料采购计划 inner join 材料 on 材料.ID=分厂上报辅料采购计划.辅料ID where 分厂ID='"+fenchang+"'";
            this.SqlDataSource1.SelectCommand = sql;
        }
        protected void GridView1_DataBound(object sender, EventArgs e)
        {
            this.TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            this.Label_Pages.Text = " 共" + this.GridView1.PageCount.ToString() + "页 ";

            this.Button_Pager_First.CssClass = "pageturn-first-b";
            this.Button_Pager_First.Enabled = true;
            this.Button_Pager_Prev.CssClass = "pageturn-prev-b";
            this.Button_Pager_Prev.Enabled = true;
            this.Button_Pager_Next.CssClass = "pageturn-next-b";
            this.Button_Pager_Next.Enabled = true;
            this.Button_Pager_Last.CssClass = "pageturn-last-b";
            this.Button_Pager_Last.Enabled = true;
            if (this.GridView1.PageIndex == 0)
            {
                this.Button_Pager_Prev.CssClass = "pageturn-prev-g";
                this.Button_Pager_Prev.Enabled = false;
            }
            if (this.GridView1.PageIndex < 2)
            {
                this.Button_Pager_First.CssClass = "pageturn-first-g";
                this.Button_Pager_First.Enabled = false;
            }
            if (this.GridView1.PageIndex == this.GridView1.PageCount - 1 || this.GridView1.PageCount == 0)
            {
                this.Button_Pager_Next.CssClass = "pageturn-next-g";
                this.Button_Pager_Next.Enabled = false;

            }
            if (this.GridView1.PageIndex > this.GridView1.PageCount - 3)
            {
                this.Button_Pager_Last.CssClass = "pageturn-last-g";
                this.Button_Pager_Last.Enabled = false;
            }

        }
        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            this.GridView1_DataBound(null, null);
        }
        protected void Pager_Click(object sender, EventArgs e)
        {
            if (sender.GetType().ToString() == "System.Web.UI.WebControls.Button")
            {
                switch (((Button)sender).CommandName)
                {
                    case "--":
                        this.GridView1.PageIndex = 0;
                        break;
                    case "-":
                        if (this.GridView1.PageIndex > 0) this.GridView1.PageIndex--;
                        break;
                    case "+":
                        if (this.GridView1.PageIndex < this.GridView1.PageCount - 1) this.GridView1.PageIndex++;
                        break;
                    case "++":
                        this.GridView1.PageIndex = this.GridView1.PageCount - 1;
                        break;
                }
            }
            else
            {

                int p;
                if (int.TryParse(TextBox_Pager.Text, out p))
                {
                    if (p >= 1 && p <= this.GridView1.PageCount)
                    {
                        this.GridView1.PageIndex = p - 1;
                    }
                }
                TextBox_Pager.Text = (this.GridView1.PageIndex + 1).ToString();
            }

        }
          public void DataBounding()
        {
            //清空计划采购量
            this.ShangBaoCaiGouLiang.Text = "";
            SqlConnection Conn = new SqlConnection();
            Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;          
            Conn.Close();
            Conn.Dispose();
        }

          protected void Button_Delete_Click(object sender, EventArgs e)
          {

          }

          protected void FuLiao_SelectedIndexChanged(object sender, EventArgs e)
          {

          }

          protected void Button_New_Click(object sender, EventArgs e)
          {
              this.Panel2.Visible = true;
              this.Button_New.Visible = false;
              this.Button_Save.Visible = true;
          }

          protected void Button_Edit_Click(object sender, EventArgs e)
          {
              if (GridView1.SelectedIndex >= 0 && GridView1.Rows.Count > 0)
              {                
              //   Response.Redirect("/Sale/分厂上报辅料计划.aspx?fenchang=" + Request.QueryString["fenchang"] + "&&ID=" + GridView1.DataKeys[GridView1.SelectedIndex].Values[0].ToString());
                  SqlConnection Conn = new SqlConnection();
                  Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
                  String strSql1 = "select 年份,月份,[品名]+' '+ [规格]+'('+[单位]+')' as [品名规格],分厂ID,上报计划采购量 from 分厂上报辅料采购计划 inner join 材料 on 材料.ID=分厂上报辅料采购计划.辅料ID where ID='" + Request.QueryString[ID] + "'";                
                  SqlDataAdapter sda = new SqlDataAdapter(strSql1, Conn);             
                  DataSet ds = new DataSet();             
                  sda.Fill(ds);

                  this.Panel2.Visible = true;
                  this.YueFen.Enabled = false;
                  this.NianDu.Enabled = false;
                  this.FuLiao.Enabled = false;
                  this.FenChang.Enabled = false;
                  this.Button_New.Visible = false;
                  this.Button_Save.Visible = true;
              }
              else
              {
                  this.Message.Text = "·请先选择一个项目";
              }
          }

          protected void Button_Save_Click(object sender, EventArgs e)
          {
              SqlConnection Conn = new SqlConnection();
              Conn.ConnectionString = ConfigurationManager.ConnectionStrings["SCMConnectionString1"].ConnectionString;
              Conn.Open();
              SqlTransaction Tran = Conn.BeginTransaction();
              SqlCommand Cmd = new SqlCommand();
              Cmd.Connection = Conn;
              Cmd.Transaction = Tran;
              try
              {
                  if (this.ShangBaoCaiGouLiang.Text != "")
                  {
                      string sql =
                        "if EXISTS(select * from 分厂上报辅料采购计划 where 分厂ID=" + this.FenChang.Text + " and 辅料ID='" + this.FuLiao.Text + "'and 年份=" + this.NianDu.Text + " and 月份=" + this.YueFen.Text + ")\n"
                        + "begin\n"
                        + "return\n"
                        + "end\n"
                        + "else\n"
                        + "begin\n"
                        + "insert into 分厂上报辅料采购计划 values(@辅料ID,@分厂ID,@上报计划采购量,@实际制定采购量,@上报日期,@年份,@月份)\n"
                        + "end";
                      sql = sql.Replace("@辅料ID", this.FuLiao.Text);
                      sql = sql.Replace("@分厂ID", this.FenChang.Text);
                      sql = sql.Replace("@上报计划采购量", this.ShangBaoCaiGouLiang.Text);
                      sql = sql.Replace("@实际制定采购量", "0");
                      sql = sql.Replace("@上报日期", "'" + DateTime.Today.ToString() + "'");
                      sql = sql.Replace("@年份", this.NianDu.Text);
                      sql = sql.Replace("@月份", this.YueFen.Text);
                      Cmd.CommandText = sql;

                      if (Cmd.ExecuteNonQuery() < 1)
                      {
                          this.Message.Text = "·保存失败，该月份上报的计划已经存在";
                          //if (this.NianDu.Enabled)
                          //{
                          //    this.Message.Text = "·保存失败，计划已经存在";
                          //}
                          //else
                          //{
                          //    this.Message.Text = "·保存失败，您正在修改的计划可能已经被删除";
                          //}
                      }
                      else
                      {
                          this.Message.Text = "·保存成功";
                          //this.NianDu.Enabled = false;
                          //this.YueFen.Enabled = false;
                      }
                  }
                  else
                  {
                      this.Message.Text = "·保存失败,请输入上报的计划量！";
                      return;
                  }

                  Tran.Commit();
              }
              catch (Exception err)
              {
                  Tran.Rollback();
                  this.Message.Text = err.Message;
              }
              Cmd.Dispose();
              Tran.Dispose();
              Conn.Close();
              Conn.Dispose();
              int fenchang = int.Parse(Request.QueryString["fenchang"]);
              GridView1_Bounding(fenchang);
          }

    }
}
